<template>
  <div class="login-container">
    <div class="login-box">
      <div style="padding:20px;background-color: white;margin-left: 100px;border-radius: 25px;box-shadow: 0 0 10px rgba(0,0,0,0.2)">
        <el-form ref="formRef" :rules="data.rules" :model="data.form"  style="width: 350px">
          <div style="margin-bottom: 30px;font-size: 24px;color: #0742b1;font-weight: bold; text-align:center" >欢迎登录租宿管理系统</div>
          <el-form-item prop="username">
            <el-input size="large" v-model="data.form.username" placeholder="请输入账号" prefix-icon="User"></el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input show-password size="large" v-model="data.form.password" placeholder="请输入密码" prefix-icon="Lock"></el-input>
          </el-form-item>

          <el-form-item prop="repassword">
            <el-input show-password size="large" v-model="data.form.repassword" placeholder="请再次输入密码" prefix-icon="Lock"></el-input>
          </el-form-item>

          <el-form-item prop="name">
            <el-input  size="large" v-model="data.form.name" placeholder="请输入姓名" prefix-icon="User"></el-input>
          </el-form-item>

          <el-form-item >
            <el-radio-group  size="large" v-model="data.form.role" >
              <el-radio label="用户" ></el-radio>
              <el-radio label="房东" ></el-radio>
            </el-radio-group>
          </el-form-item>

          <div>
            <el-button size="large" style="width: 100%" type="primary" @click="register">注册</el-button>
          </div>
          <div style="text-align:right">已有账号？请
            <a style="color: #0742b1;text-decoration: none" href="/login">登录</a>
          </div>



        </el-form>
      </div>

    </div>
  </div>
</template>

<script setup>

import { reactive ,ref} from "vue";
import {ElMessage} from "element-plus"
import {User,Lock} from "@element-plus/icons-vue"
import request from "../utils/request";



const validatePass = (rule, value, callback) => {
  if (!value) {
    callback(new Error('请再次输入密码'))
  } else if (value !== data.form.password){
    callback(new Error('两次输入的密码不一致!'))
  } else {
    callback()
  }

}

const data = reactive({
  form:{
    role:'用户',
    avatar:"https://house1205.oss-cn-shenzhen.aliyuncs.com/55ce8bfa-6d7d-4a5b-9abe-d8e2ccb22b67.png"
  },
  rules:{
    username: [
      { required: true, message: '请输入账号', trigger: 'blur' },
      { min: 5, max: 11, message: '长度在 5 到 11 个字符', trigger: 'blur' }
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      { min: 5, max: 11, message: '长度在 5 到 11 个字符', trigger: 'blur' }
    ],
    repassword:[
      {validator:validatePass, trigger: 'blur'}
    ],
    name: [
      { required: true, message: '请输入姓名', trigger: 'blur' },
      { min: 2, max: 6, message: '长度在 2 到 6 个字符', trigger: 'blur' }
    ]
  }
})

const formRef = ref()

const register = () => {
  formRef.value.validate((valid) => {
    if (valid){
      if (data.form.role==='用户'){
        request.post('user/register',data.form).then(res => {

          if (res.code === '200'){
            console.log(res.data)
            //注册成功
            ElMessage.success('注册成功')

            //存储后台返回的用户数据信息
            localStorage.setItem('adminInfo',JSON.stringify(res.data))//把json对象转换成json字符串存储

            setTimeout(()=>{
              location.href = '/login'
            },500)


          }else {
            ElMessage.error(res.msg)
          }
        })
      }else if (data.form.role==='房东'){
        request.post('owner/register',data.form).then(res => {

          if (res.code === '200'){
            console.log(res.data)
            //注册成功
            ElMessage.success('注册成功')

            //存储后台返回的用户数据信息
            localStorage.setItem('adminInfo',JSON.stringify(res.data))//把json对象转换成json字符串存储

            setTimeout(()=>{
              location.href = '/login'
            },500)


          }else {
            ElMessage.error(res.msg)
          }
        })
      }

    }

  })
}


</script>

<style scoped>
.login-container {
  height: 100vh;
  overflow: hidden;
  background-image: url("src/assets/imgs/loginBG.png");
  background-size: 100% 100%;
}
.login-box{
  width: 50%;
  height: 100%;
  display: flex;
  align-items: center;
  right: 0;
  position: absolute;
}
</style>